package com.bustle.web.mbean.request;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import com.bustle.ejb.service.AuthenticationException;
import com.bustle.ejb.service.AuthenticationManager;
import com.bustle.web.Report;
import com.bustle.web.mbean.session.UserMBean;

@ManagedBean
@RequestScoped
public class IdentificationMBean
{
  public String go()
  {
    return SCREEN;
  }

  public String connect()
  {
    try
    {
      userMBean.setId(authenticationManager.authenticate(login, password).getId());
      return "monCompte.jsf";
    }
    catch (AuthenticationException anException)
    {
      Report.error("Unable to connect login " + login, anException);
    }
    return null;
  }

  public String getLogin()
  {
    return login;
  }

  public void setLogin(String aLogin)
  {
    login = aLogin;
  }

  public String getPassword()
  {
    return password;
  }

  public void setPassword(String aPassword)
  {
    password = aPassword;
  }

  public void setUserMBean(UserMBean aUserMBean)
  {
    userMBean = aUserMBean;
  }

  protected String login;
  protected String password;

  @EJB
  protected AuthenticationManager authenticationManager;
  @ManagedProperty("#{userMBean}")
  protected UserMBean userMBean;

  static public final String SCREEN = "identification.jsf";
}
